package com.yzg.thread;

import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import java.util.concurrent.TimeUnit;

/**
 * @author misterWei
 * @create 2020年04月10号:21点39分
 * @mailbox forcontinue@163.com
 */
@Slf4j(topic = "c.method")
public class ThreadMethod {

    @Test
    public void threadSleep() throws InterruptedException {
        Thread thread = new Thread(()->{

            try {
                log.info("线程准备沉睡...");
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                log.info("线程被唤醒...");
            }
        },"t1");

        log.debug("线程状态为{}",thread.getState());
        thread.start();
        log.debug("main 线程");
        TimeUnit.MICROSECONDS.sleep(100);
        log.debug("线程状态为{}",thread.getState());
        //唤醒沉睡线程
        thread.interrupt();
    }

    @Test
    public void yieldTest() throws InterruptedException {
        Thread thread1 = new Thread(()->{
            int count = 0;

            while (true){
                log.debug("线程1-->{}",count ++);
            }

        },"t1");


        Thread thread2 = new Thread(()->{

            int count = 0;
            while (true){
                //让出执行权
                log.debug("           线程2-->{}",count ++);
            }

        },"t2");
        //1 优先级最高
        thread1.setPriority(Thread.MIN_PRIORITY);
        //10 优先级最后
        thread2.setPriority(Thread.MAX_PRIORITY);
        thread1.start();
        thread2.start();

        //测试方法需要将测试线程进行等待
        TimeUnit.NANOSECONDS.sleep(10000100);
    }

     int  count = 0;
    @Test
    public void joinTest() throws InterruptedException {

        Thread thread1 = new Thread(()->{
               count = 10;
        },"t1");
         thread1.start();
         // join 方法保证 thread1这个线程执行完毕之后才继续执行
         thread1.join();
        log.debug("执行结果-->{}",count);

        //测试方法需要将测试线程进行等待
        TimeUnit.NANOSECONDS.sleep(10000100);
    }

    @Test
    public void interruptedTest() throws InterruptedException {
        Thread thread1 = new Thread(()->{
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                log.debug("t1线程被打断... 状态为{}",Thread.currentThread().isInterrupted());
                e.printStackTrace();
            }
        },"t1");
        thread1.start();
        thread1.interrupt();
        //测试方法需要将测试线程进行等待
        TimeUnit.NANOSECONDS.sleep(10000100);
    }

    @Test
    public void interruptedTest2() throws InterruptedException {
        Thread thread1 = new Thread(()->{
           while (true){
               boolean interrupted = Thread.currentThread().isInterrupted();
               if (interrupted){
                   log.debug("t1线程被打断... 状态为{}",interrupted);
                   break;
               }
           }
        },"t1");
        thread1.start();
        thread1.interrupt();
        /**
         *  Thread.interrupted() 也是用来打断的,只不过它会将返回值恢复默认,也就是false
         */

        //测试方法需要将测试线程进行等待
        TimeUnit.NANOSECONDS.sleep(10000100);
    }

}
